﻿///<reference path="../../vswd-ext_2.0.2.js" />
Ext.BLANK_IMAGE_URL = '../../Images/s.gif';

//ArgType
var argtypeDs = new Ext.data.Store({
    url: '../../Trans/Base/Arg.aspx',
    baseParams: {
        action: 'typelist'
    },
    remoteSort: true,
    reader: new Ext.data.JsonReader({
        root: 'rows',
        totalProperty: 'rowsCount'
    },[
        'ArgTypeID',
        'ArgTypeName_cn',
		'ArgTypeName_en',
		'ArgTypeName_ja',
        'ArgTypeMark',
        'SortIndex',
        'CreateTime',
        'UpdateTime'
    ])
});



Ext.onReady(function(){
    Ext.QuickTips.init();
    
    var urlArgObj = Ext.urlDecode(window.location.search.substring(1, window.location.search.length));
    var tabHeight = urlArgObj['h']-1; //the height of tab panel
    var tabWidth = urlArgObj['w']-1; //the width of tab panel
    var pageId = urlArgObj['page_id']; //the function id of the page
    
    Ext.Ajax.request({
        url: '../../Trans/Base/Lang.aspx',
        params: {
            action: 'lang',
            marks: "'col_arg_name_cn','col_arg_name_en','col_arg_name_ja','col_arg_mark','msg_must_select_one','msg_select_one_to_del','btn_cancel','msg_confirm_to_del','msg_select_one_to_eidt','btn_sure','title_tip','col_arg_type_name','col_arg_type_mark','col_sort_index','col_create_time','col_update_time','grid_panel_display_msg','grid_panel_empt_msg','max_length_text','blank_text','btn_save','btn_close','combo_empty_text','title_add','lbl_user_name','col_arg_type_name_en','col_arg_type_name_ja','msg_invalid_number'"
        },
        success: function(response, opts) {
            var langObj = Ext.decode(response.responseText);
            langObj = langObj['rows'][0];
            
            //加载数据
            argtypeDs.load();       

			//ArgType表单项 begin			
			
			//SortIndex
            Ext.form.VTypes['SortIndexVal'] = /^[0-9]\d*$/;
            Ext.form.VTypes['SortIndexText'] = langObj ['msg_invalid_number'];
            Ext.form.VTypes['SortIndex'] = function(v) {
                if (v !=  '') {
                    return Ext.form.VTypes['SortIndexVal'].test(v);
                }
                return true;
            };
			
			
			var p1 = new Ext.Panel({
				layout: 'form',
				border:false,
				width:300,
				defaultType: 'textfield',
				bodyStyle:'padding:5px 5px 0;background:transparent;',
				items:[
				{
					xtype: 'hidden',
					name: 'ArgTypeID'
				},
				{  
					fieldLabel:langObj['col_arg_type_name'],  
					name:'ArgTypeName_cn', 
					width: 120,
					maxLength: 30,
					maxLengthText: langObj['max_length_text'],
					allowBlank: false,
					blankText: langObj['blank_text']
				},  
				{  
					fieldLabel:langObj['col_arg_type_name_en'],  
					name:'ArgTypeName_en',
					width: 120,
					maxLength: 30,
					maxLengthText: langObj['max_length_text'],
					allowBlank: false,
					blankText: langObj['blank_text']
				},  
				{  
					fieldLabel:langObj['col_arg_type_name_ja'],  
					name:'ArgTypeName_ja',
					width: 120,
					maxLength: 30,
					maxLengthText: langObj['max_length_text'],
					allowBlank: false,
					blankText: langObj['blank_text']
				},  
				{  
					fieldLabel:langObj['col_arg_type_mark'],  
					name:'ArgTypeMark',
					width: 120,
					maxLength: 30,
					maxLengthText: langObj['max_length_text'],
					allowBlank: false,
					blankText: langObj['blank_text']
				},  
				{  
					fieldLabel:langObj['col_sort_index'],  
					name:'SortIndex',
					width: 120,
					maxLength: 30,
					maxLengthText: langObj['max_length_text'],
					allowBlank: false,
					blankText: langObj['blank_text'],
					vtype: 'SortIndex'
				}]
			});
			
			var action = null; //操作
			
			//参数类型的Panel
			var argtypeFormPanel = new Ext.FormPanel({
				id:'argtype',
				border:false,
				width:300,
				labelWidth: 100,
				labelAlign: 'right',
				buttonAlign: 'center',
				bodyStyle: 'padding:5px 5px 0;background:transparent;',
				items: [p1],
				buttons:[{
					id:'btn_save',
					text:langObj['btn_save'],
					width:60,
					handler:function() {
                        var argtypeForm = argtypeFormPanel.getForm();
						if(argtypeForm.isValid()){
							win.hide(null,function(){
								var model = '{';
								model += "'ArgTypeNameCn':'" + argtypeForm.findField('ArgTypeName_cn').getValue() +"',";
								model += "'ArgTypeNameEn':'" + argtypeForm.findField('ArgTypeName_en').getValue() +"',";
								model += "'ArgTypeNameJa':'" + argtypeForm.findField('ArgTypeName_ja').getValue() +"',";
								model += "'ArgTypeMark':'" + argtypeForm.findField('ArgTypeMark').getValue() +"',";
								model += "'SortIndex':'" + argtypeForm.findField('SortIndex').getValue() +"',";
								model += "'ArgTypeId':'" + argtypeForm.findField('ArgTypeID').getValue() +"'}";
								
								Ext.Ajax.request({
									url:'../../Trans/Base/Arg.aspx',
									params: {
										action: action,
										model: model
									},
									success:function(response,opts){
										var obj = Ext.decode(response.responseText);
										if (obj['success'] == true) {
										Ext.Msg.show({
											title: langObj['title_tip'],
											msg: obj['msg'],
											buttons: {yes: langObj['btn_sure']},
											icon: Ext.MessageBox.INFO
										});
										
										argtypeDs.reload();
									}else {
										Ext.Msg.show({
											title: langObj['title_tip'],
											msg: obj['msg'],
											buttons: {yes: langObj['btn_sure']},
											icon: Ext.MessageBox.ERROR
										});
									}
								},
								failure: function(response, opts) {
									var obj = Ext.decode(response.responseText);
									Ext.Msg.show({
										title: langObj['title_tip'],
										msg: obj['msg'],
										buttons: {yes: langObj['btn_sure']},
										icon: Ext.MessageBox.ERROR
									});
								}
								});
							},this);
						}
					}
				},{
					text: langObj['btn_close'],
					width: 60,
					handler: function() {
						win.hide(null, null, this);
					}
				}]
			});
			
			//显示窗体
			var win = new Ext.Window({
				modal: true,
				resizable: false,
				width: 300,
				height: 220,
				items: [argtypeFormPanel]
			});
			
            //ArgType表单项 end
			
			//给用户表单设置初始值
            function argtypeFormSetDefault() {
                var argtypeForm = argtypeFormPanel.getForm();
                
                argtypeForm.items.each(function(item, index, lenght) {
                    item.enable();
                    
                    if (item.getXType() == 'combo') {
                        item.setRawValue(langObj['combo_empty_text']);
                    } else {
                        item.setRawValue();
                    }
                }, this);
                if(argtypeFormPanel.getFooterToolbar().findById('btn_save'))
				{
					argtypeFormPanel.getFooterToolbar().findById('btn_save').enable(); //激活保存按钮
				}
            }
			
			//ArgType的checkbox
            var checkboxSel = new Ext.grid.CheckboxSelectionModel();
			//Arg的checkbox
			var argcheckboxSel = new Ext.grid.CheckboxSelectionModel();
            
			//ArgType的Grid
            var argTypeGrid = new Ext.grid.GridPanel({
                id: 'argTypeGrid',
                renderTo: document.body,
                frame: false,
                height: tabHeight,
                store: argtypeDs,
                loadMask: true,
                sm: checkboxSel,
                bbar: new Ext.PagingToolbar({
                    pageSize: 20,
                    store: argtypeDs,
                    displayInfo: true,
                    displayMsg: langObj['grid_panel_display_msg'],
                    emptMsg: langObj['grid_panel_empt_msg']
                }),
                columns: [new Ext.grid.RowNumberer(),checkboxSel,{
                    header: 'ArgTypeID',
                    dataIndex: 'ArgTypeID',
                    hidden: true,
                    sortable: true
                },{
                    header: langObj['col_arg_type_name'],
                    dataIndex: 'ArgTypeName_cn',
                    sortable: true,
                    width: 100
                },{
                    header: langObj['col_arg_type_name_en'],
                    dataIndex: 'ArgTypeName_en',
                    sortable: true,
                    width: 100
                },{
                    header: langObj['col_arg_type_name_ja'],
                    dataIndex: 'ArgTypeName_ja',
                    sortable: true,
                    width: 100
                },{
                    header: langObj['col_arg_type_mark'],
                    dataIndex: 'ArgTypeMark',
                    sortable: true,
                    width: 100
                },{
                    header: langObj['col_sort_index'],
                    dataIndex: 'SortIndex',
                    sortable: true,
                    width: 50
                },{
                    header: langObj['col_create_time'],
                    dataIndex: 'CreateTime',
                    sortable: true,
                    width: 130
                },{
                    header: langObj['col_update_time'],
                    dataIndex: 'UpdateTime',
                    sortable: true,
                    width: 130
                }]
            });
			
			//Arg的Grid
            var argGrid = new Ext.grid.GridPanel({
                id: 'argGrid',
                renderTo: document.body,
                frame: false,
                height: tabHeight,
                store: argDs,
                loadMask: true,
                sm: argcheckboxSel,
                // bbar: new Ext.PagingToolbar({
                    // pageSize: 20,
                    // store: argDs,
                    // displayInfo: true,
                    // displayMsg: langObj['grid_panel_display_msg'],
                    // emptMsg: langObj['grid_panel_empt_msg']
                // }),
                columns: [new Ext.grid.RowNumberer(),argcheckboxSel,{
                    header: 'ArgId',
                    dataIndex: 'ArgId',
                    hidden: true,
                    sortable: true
                },{
                    header: 'ArgTypeId',
                    dataIndex: 'ArgTypeId',
                    hidden: true,
                    sortable: true
                },{
                    header: langObj['col_arg_mark'],
                    dataIndex: 'ArgMark',
                    sortable: true,
                    width: 100
                },{
                    header: langObj['col_arg_name_cn'],
                    dataIndex: 'ArgNameCn',
                    sortable: true,
                    width: 100
                },{
                    header: langObj['col_arg_name_en'],
                    dataIndex: 'ArgNameEn',
                    sortable: true,
                    width: 100
                },{
                    header: langObj['col_arg_name_ja'],
                    dataIndex: 'ArgNameJa',
                    sortable: true,
                    width: 100
                },{
                    header: langObj['col_sort_index'],
                    dataIndex: 'SortIndex',
                    sortable: true,
                    width: 50
                },{
                    header: langObj['col_create_time'],
                    dataIndex: 'CreateTime',
                    sortable: true,
                    width: 130
                },{
                    header: langObj['col_update_time'],
                    dataIndex: 'UpdateTime',
                    sortable: true,
                    width: 130
                }]
            });
            
             //刷新
            function argtypeRefresh() {
                argtypeDs.reload();
            }
            
            //添加
            function argtypeAdd() {				
				argtypeFormSetDefault();                
                action = 'add';				
				win.setTitle(langObj['title_add']);				
                win.show(null,null,this);
            }
            
             //查看
            function argtypeView() {
                if (checkboxSel.hasSelection()) {
                    var selections = checkboxSel.getSelections();
                    if (selections.length > 1) {
                        Ext.Msg.show({
                            title: langObj['title_tip'],
                            msg: langObj['msg_select_one_to_view'],
                            buttons: {yes: langObj['btn_sure']},
                            icon: Ext.MessageBox.INFO
                        });
                    } else {
                        Ext.Ajax.request({
                            url: '../../Trans/Base/Arg.aspx',
                            params: {
                                action: 'model',
                                sel_argtype_id: selections[0].data.ArgTypeID
                            },
                            success: function(response, opts) {
                                var obj = Ext.decode(response.responseText);
                                if (obj['success'] == true) {
                                    var argtypeModel = obj['rows'];
                                    
                                    argtypeFormSetDefault();
									var argtypeForm = argtypeFormPanel.getForm();
									
									argtypeForm.items.each(function(item,index,lenght){
										item.disable();
									},this);
									
                                    argtypeForm.findField('ArgTypeMark').setValue(argtypeModel['ArgTypeMark']);
                                    argtypeForm.findField('ArgTypeName_cn').setValue(argtypeModel['ArgTypeNameCn']);
                                    argtypeForm.findField('ArgTypeName_en').setValue(argtypeModel['ArgTypeNameEn']);
                                    argtypeForm.findField('ArgTypeName_ja').setValue(argtypeModel['ArgTypeNameJa']);
                                    argtypeForm.findField('SortIndex').setValue(argtypeModel['SortIndex']);
									argtypeForm.findField('ArgTypeID').setValue(argtypeModel['ArgTypeId']);     
                                    
                                    argtypeFormPanel.getFooterToolbar().findById('btn_save').disable(); //禁止保存按钮
                                    
                                    win.setTitle(langObj['title_view']);
                                    win.show(null,null,this);
                                } else {
                                    Ext.Msg.show({
                                        title: langObj['title_tip'],
                                        msg: obj['msg'],
                                        buttons: {yes: langObj['btn_sure']},
                                        icon: Ext.MessageBox.ERROR
                                    });
                                }
                            },
                            failure: function(response, opts) {
                                var obj = Ext.decode(response.responseText);
                                Ext.Msg.show({
                                    title: langObj['title_tip'],
                                    msg: obj['msg'],
                                    buttons: {yes: langObj['btn_sure']},
                                    icon: Ext.MessageBox.ERROR
                                });
                            }
                        });
                    }
                } else {
                    Ext.Msg.show({
                        title: langObj['title_tip'],
                        msg: langObj['msg_select_one_to_view'],
                        buttons: {yes: langObj['btn_sure']},
                        icon: Ext.MessageBox.INFO
                    });
                }
            }
            
             //编辑
            function argtypeEdit() {
             if (checkboxSel.hasSelection()) {
                    var selections = checkboxSel.getSelections();
                    if (selections.length > 1) {
                        Ext.Msg.show({
                            title: langObj['title_tip'],
                            msg: langObj['msg_select_one_to_eidt'],
                            buttons: {yes: langObj['btn_sure']},
                            icon: Ext.MessageBox.INFO
                        });
                    } else {
                        Ext.Ajax.request({
							url:'../../Trans/Base/Arg.aspx',
                            params: {
                                action: 'model',
                                sel_argtype_id: selections[0].data.ArgTypeID
                            },
                            success: function(response, opts) {
                                var obj = Ext.decode(response.responseText);
                                if (obj['success'] == true) {
                                    var argtypeModel = obj['rows'];
                                    
                                    argtypeFormSetDefault();
                                    action = 'edit';
                                    var argtypeForm = argtypeFormPanel.getForm();
                                    argtypeForm.findField('ArgTypeMark').setValue(argtypeModel['ArgTypeMark']);
                                    argtypeForm.findField('ArgTypeMark').disable();
                                    argtypeForm.findField('ArgTypeName_cn').setValue(argtypeModel['ArgTypeNameCn']);
                                    argtypeForm.findField('ArgTypeName_en').setValue(argtypeModel['ArgTypeNameEn']);
                                    argtypeForm.findField('ArgTypeName_ja').setValue(argtypeModel['ArgTypeNameJa']);
                                    argtypeForm.findField('SortIndex').setValue(argtypeModel['SortIndex']);
									argtypeForm.findField('ArgTypeID').setValue(argtypeModel['ArgTypeId']);
                                    
                                    win.setTitle(langObj['title_edit']);
                                    win.show(null,null,this);
                                } else {
                                    Ext.Msg.show({
                                        title: langObj['title_tip'],
                                        msg: obj['msg'],
                                        buttons: {yes: langObj['btn_sure']},
                                        icon: Ext.MessageBox.ERROR
                                    });
                                }
                            },
                            failure: function(response, opts) {
                                var obj = Ext.decode(response.responseText);
                                Ext.Msg.show({
                                    title: langObj['title_tip'],
                                    msg: obj['msg'],
                                    buttons: {yes: langObj['btn_sure']},
                                    icon: Ext.MessageBox.ERROR
                                });
                            }
                        });
                    }
					} else {
						Ext.Msg.show({
							title: langObj['title_tip'],
							msg: langObj['msg_select_one_to_eidt'],
							buttons: {yes: langObj['btn_sure']},
							icon: Ext.MessageBox.INFO
						});
					}
				}
			
            
             //删除
            function argtypeLogicDel() {
                if (checkboxSel.hasSelection()) {
                    Ext.Msg.show({
                        title: langObj['title_tip'],
                        msg: langObj['msg_confirm_to_del'],
                        buttons: {yes: langObj['btn_sure'], no: langObj ['btn_cancel']},
                        icon: Ext.MessageBox.QUESTION,
                        fn: function(button, text) {
                            if (button == 'yes') {
                                var selections = checkboxSel.getSelections();
                    
                                var selArgTypeIds = "";
                                for (var i = 0; i < selections.length; i++) {
                                    selArgTypeIds += selections[i].data.ArgTypeID.toString() + ",";
                                }
                                selArgTypeIds = selArgTypeIds.substring(0, selArgTypeIds.length - 1);
                                
                                Ext.Ajax.request({
                                    url: '../../Trans/Base/Arg.aspx',
                                    params: {
                                        action: 'logicdel',
                                        sel_argtype_id: selArgTypeIds
                                    },
                                    success: function(response, opts) {
                                        var obj = Ext.decode(response.responseText);
                                        if (obj['success'] == true) {
                                            Ext.Msg.show({
                                                title: langObj['title_tip'],
                                                msg: obj['msg'],
                                                buttons: {yes: langObj['btn_sure']},
                                                icon: Ext.MessageBox.INFO
                                            });
                                            
                                            argtypeDs.reload();
                                        } else {
                                            Ext.Msg.show({
                                                title: langObj['title_tip'],
                                                msg: obj['msg'],
                                                buttons: {yes: langObj['btn_sure']},
                                                icon: Ext.MessageBox.ERROR
                                            });
                                        }
                                    },
                                    failure: function(response, opts) {
                                        var obj = Ext.decode(response.responseText);
                                        Ext.Msg.show({
                                            title: langObj['title_tip'],
                                            msg: obj['msg'],
                                            buttons: {yes: langObj['btn_sure']},
                                            icon: Ext.MessageBox.ERROR
                                        });
                                    }
                                });
                            }
                        }
                    });
                    
                } else {
                    Ext.Msg.show({
                        title: langObj['title_tip'],
                        msg: langObj['msg_select_one_to_del'],
                        buttons: {yes: langObj['btn_sure']},
                        icon: Ext.MessageBox.INFO
                    });
                }
            }
            
			//Arg的Store
			var argDs = new Ext.data.Store({
				url:'../../Trans/Base/Arg.aspx',
				baseParams:{
					action:'arglist'
				},
				remoteSort:true,
				reader:argreader
				// reader:new Ext.data.JsonReader({
					// root:'rows',
					// totalProperty:'rowsCount'
				// },[
					// 'ArgId',
					// 'ArgMark',
					// 'ArgTypeId',
					// 'ArgNameCn',
					// 'ArgNameEn',
					// 'ArgNameJa',
					// 'Status',
					// 'SortIndex',
					// 'CreateUser',
					// 'CreateTime',
					// 'UpdateUser',
					// 'UpdateTime'
				// ])
			});
			
			//参数管理
			function argdetail()
			{
				 if (checkboxSel.hasSelection()) {
                    var selections = checkboxSel.getSelections();
                    if (selections.length > 1) {
                        Ext.Msg.show({
                            title: langObj['title_tip'],
                            msg: langObj['msg_select_one_to_eidt'],
                            buttons: {yes: langObj['btn_sure']},
                            icon: Ext.MessageBox.INFO
                        });
                    } else {
                        Ext.Ajax.request({
							url:'../../Trans/Base/Arg.aspx',
                            params: {
                                action: 'argmodel',
                                sel_argtype_id: selections[0].data.ArgTypeID
                            },
                            success: function(response, opts) {
                                var obj = Ext.decode(response.responseText);
                                if (obj['success'] == true) {
                                    var argModel = obj['rows'];debugger;
                                    argreader = new Ext.data.JsonReader({
										root:'rows',
										totalProperty:'rowsCount'
									},[
										'ArgId',
										'ArgMark',
										'ArgTypeId',
										'ArgNameCn',
										'ArgNameEn',
										'ArgNameJa',
										'Status',
										'SortIndex',
										'CreateUser',
										'CreateTime',
										'UpdateUser',
										'UpdateTime'
									]);
                                    // argtypeFormSetDefault();
                                    // action = 'edit';
                                    // var argtypeForm = argtypeFormPanel.getForm();
                                    // argtypeForm.findField('ArgTypeMark').setValue(argtypeModel['ArgTypeMark']);
                                    // argtypeForm.findField('ArgTypeMark').disable();
                                    // argtypeForm.findField('ArgTypeName_cn').setValue(argtypeModel['ArgTypeNameCn']);
                                    // argtypeForm.findField('ArgTypeName_en').setValue(argtypeModel['ArgTypeNameEn']);
                                    // argtypeForm.findField('ArgTypeName_ja').setValue(argtypeModel['ArgTypeNameJa']);
                                    // argtypeForm.findField('SortIndex').setValue(argtypeModel['SortIndex']);
									// argtypeForm.findField('ArgTypeID').setValue(argtypeModel['ArgTypeId']);
                                    
                                    // win.setTitle(langObj['title_edit']);
                                    // win.show(null,null,this);
                                } else {
                                    Ext.Msg.show({
                                        title: langObj['title_tip'],
                                        msg: obj['msg'],
                                        buttons: {yes: langObj['btn_sure']},
                                        icon: Ext.MessageBox.ERROR
                                    });
                                }
                            },
                            failure: function(response, opts) {
                                var obj = Ext.decode(response.responseText);
                                Ext.Msg.show({
                                    title: langObj['title_tip'],
                                    msg: obj['msg'],
                                    buttons: {yes: langObj['btn_sure']},
                                    icon: Ext.MessageBox.ERROR
                                });
                            }
                        });
                    }
					} else {
						Ext.Msg.show({
							title: langObj['title_tip'],
							msg: langObj['msg_must_select_one'],
							buttons: {yes: langObj['btn_sure']},
							icon: Ext.MessageBox.INFO
						});
					}
			}
			
			
			
            //生成功能点
            Ext.Ajax.request({
                url: '../../Trans/Base/Function.aspx',
                params: {
                    action: 'func_points',
                    page_id: pageId
                },
                success: function(response, opts) {
                    var obj = Ext.decode(response.responseText);
                    
                    if (obj['success'] == true) {
                        var funcPointsObj = obj['rows'];
                        var tb = new Ext.Toolbar();
                        for (var propName in funcPointsObj) {
                            switch (propName) {
                                case EF.UI.Const.FuncPoints.REFRESH:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: argtypeRefresh
                                    });
                                break;
                                case EF.UI.Const.FuncPoints.ADD:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: argtypeAdd
                                    });
                                break; 
                                case EF.UI.Const.FuncPoints.EDIT:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: argtypeEdit
                                    });
                                break;
                                case EF.UI.Const.FuncPoints.VIEW:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: argtypeView
                                    });
                                break;                            
                                case EF.UI.Const.FuncPoints.LOGICDEL:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: argtypeLogicDel
                                    });
                                break;
								case EF.UI.Const.FuncPoints.ARGDETAIL:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: argdetail
                                    });
                                break;
                                ;
                            }
                        }
                        
                        argTypeGrid.add(tb);
                        argTypeGrid.doLayout();
                    }
                },
                failure: function() {
                
                }
            });
            
            
            
        },
        failure: function() {
        
        }
    }); 
});

